'use strict';

/*
 * @Author: Kang
 * @Date: 2022-02-11 22:41:56
 * @Last Modified by: Kang
 * @LastEditTime: 2022-09-05 11:07:39
 */
module.exports = app => {
	/**
	* @controller LoginController (登录的Controller)
	*/
	class LoginController extends app.Controller {

		* index() {
			try {
				const { ctx, app } = this;
				console.log('ctx', ctx.request.body)
				//获取登录时的 usename，password
				const { username, password } = ctx.request.body;
				//根据用户名，在数据库查找相对应的id操作
				const userInfo = yield this.service.login.getUserByName(username);
				// 1. 没找到说明没有该用户
				if (!userInfo || !userInfo.id) {
					ctx.body = {
						code: 500,
						desc: '账号不存在',
						data: null,
					}
					return
				}
				// 2. 找到用户，并且判断输入密码与数据库中用户密码
				if (userInfo && password != userInfo.password) {
					ctx.body = {
						code: 'error',
						desc: '账号密码错误',
						data: null
					}
					return
				} else {

					const token = app.jwt.sign({
						id: userInfo.id,
						username: userInfo.username,
					}, app.config.jwt.secret, {
						expiresIn: app.config.jwt.expiresIn,
					});

					ctx.body = {
						code: 200,
						desc: '登录成功',
						currentAuthority: "admin",
						type: "account",
						data: { token, username: userInfo.username }
					}
				}


			} catch (error) {
				ctx.body = {
					status: 500,
					desc: '登录失败',
					data: null
				}
			}
		}

		* currentUser() {
			const { ctx } = this;
			ctx.body = {
				success: true,
				data: {
					name: '1',
					avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
					userid: '00000001',
					email: 'antdesign@alipay.com',
					signature: '海纳百川，有容乃大',
					title: '交互专家',
					group: '蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED',
					tags: [
						{
							key: '0',
							label: '很有想法的',
						},
						{
							key: '1',
							label: '专注设计',
						},
						{
							key: '2',
							label: '辣~',
						},
						{
							key: '3',
							label: '大长腿',
						},
						{
							key: '4',
							label: '川妹子',
						},
						{
							key: '5',
							label: '海纳百川',
						},
					],
					notifyCount: 12,
					unreadCount: 11,
					country: 'China',
					access: 'admin',
					geographic: {
						province: {
							label: '浙江省',
							key: '330000',
						},
						city: {
							label: '杭州市',
							key: '330100',
						},
					},
					address: '西湖区工专路 77 号',
					phone: '0752-268888888',
				},
			}
		}
	}
	return LoginController;
}